﻿$(function() {
	$('#choosecompany').autocomplete(base + 'task.ivp/findcompanies').result(companySelected);
    if ($('#readonly').length > 0) {
        $('input,textarea').attr('disabled', true).addClass('disabled');
        $('select').attr('disabled', true).addClass('disabled');
    }
    if ($('#returntoopeneraftersave').length > 0 && $('#returntoopeneraftersave').val() == 'true')
        addCompanyToOrder($('#id').val()+'0');
});

//-----------tabs------------
function setTab(id) {
    if (!id) return;
    $('#tab').val(id);
    switch (id) {
        case 'orders':
            if ($('#orders table').length == 0) 
				loadOrders(true);
			break;            
		case 'persons':
            if ($('#persons table').length == 0) 
                loadPersons(true);         
			break;
    }
	$('.tab').hide();
	$('#' + id).show();
    $('#tabs>ul>li>a').removeClass('selected');
    $('#tabs>ul>li>a#a' + id).addClass('selected');
    return false;
}

function loadOrders(reload) {
    if (reload || !$.trim($('#orders').html()))
        $('#orders').load(base + 'order.ivp/loadcompanyorders/?reset=true', { tb_companyid: $('#id').val(), timestamp: new Date() }, function() {  setTab('orders'); });
    else setTab('orders');
    return false;
}


function loadPersons(reload) {
    if (reload || !$.trim($('#persons').html()))
        $('#persons').load(base + 'person.ivp/loadcompanypersons/', { companyid: $('#id').val(), timestamp: new Date() }, function() {  setTab('persons'); });
    else setTab('persons');
    return false;
}

//-----------------external calls-----------------
function addCompany(id) {
    if (id && id > 0) 
    {
        var name = $('#name').val();
        if (self.callback == null) self.callback = opener._personSelected;
        self.callback(id, name, 'true');
    }
    self.close();
    return false;
}

function addCompanyToOrder(id) {
    if (id && id > 0) {
        var name = $('#name').val();
        if (self.callback == null) self.callback = opener.userSelected;        
        self.callback(id, name, ($('#iscashless').val() == 'True' || $('#iscashless').attr('checked')));
    }
    self.close();
    return false;
}

//-------------------person links--------------
function choosePersons() {
    openDialog('person.ivp/select');
    return false;
}

function addSelected(personid) {
    $.post("companyperson.ivp/addcompanyperson/", { timestamp: new Date(), companyid: $('#id').val(), personid: personid}, function() {loadPersons(true);});
	return false;
}

function deletePersons() {
    var ids = new Array();
    $('#persons input[name=id]:checked').each(function(i, el) { ids.push(el.value); });
    if (ids.length == 0) { alert('Ни одна запись не выбрана!'); return false; }
    if (!confirm('Вы уверены, что ходите удалить выбранные записи?')) return false;
    $.post(base + 'companyperson.ivp/deletepersoncompany', { timestamp: new Date(), companyid: $('#id').val(), ids: ids}, function() { loadPersons(true);});
    return false;
}

//-------------------union--------------------

function selectCompany() {
    var dlg = openDialog('company.ivp/select?reset=true');
    dlg.callback = function(id) { _companySelected(id, dlg.userNames['un' + id.toString()]); }
    return false;
}

function companySelected(e, d) {
    var id = d[1];
    var name = d[0];
    $(e.target).val('');
    _companySelected(id, name);
}

function _companySelected(id, name, hideselect) {
	loadUnionSource(id);
    return false;
}

function unionField(field) {
	var src = $('#source #'+field.toString());	
	var dest = $('#destination #'+field.toString());
	if (src.val().length > 0)
		dest.val(dest.val() + ' ' + src.val());
	return false;
}

function replaceField(field) {
	var src = $('#source #'+field.toString());	
	var dest = $('#destination #'+field.toString());
	if ($('#source #' + field.toString()).attr('type') == 'checkbox')
		dest.attr('checked',src.attr('checked'));
	else
		dest.val(src.val());
	return false;
}

function loadUnionSource(id) {
	$('#source').load(base + 'company.ivp/loadunionsource/'+id, { nolayout: true, timestamp: new Date()},
		function() {$('#source input, #source textarea').attr('disabled', true); $('#unioncompanyid').val(id);});
	return false;
}